package behavior;

import arena.Node;
import java.util.LinkedList;
import robot.Robot;

public class GoGoRoboPath extends Behavior {
    // Attributes
    private Node nextNode;
    private LinkedList<Node> path = new LinkedList<Node>();
    boolean found = false;
    String goal;
    char mediator;
    char shape;

    // Constructors
    public GoGoRoboPath() {
        super("GoGoRoboPath");
    }
    
    @Override
    public double getProgress() {
        return 100;
    }

    @Override
    public void resetProgress() {
    }

    public boolean isValidMove() {
         if (path.size() == 0) {
            return false;
        }
        Node node = path.peek();

        if (!node.isOccupied) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void getPath(String goal, boolean cancel) {
        if (cancel) {
            found = false;
            return;
        }

        shape = goal.toUpperCase().charAt(0);

        switch (shape) {
            case 'S':
                squareTrace();
                break;
            default:
                found = false;
                break;
        }

            path.add(nextNode);
        }

    @Override
    public boolean hasMove() {
        return !path.isEmpty() && !(path.peek().isOccupied);
    }

    @Override
    public int[] nextMove() {
            Node node = path.pop();

            int dx = node.getLocation().x - robot.getLocation().x;
            int dy = node.getLocation().y - robot.getLocation().y;

            char dir;
            if (dx == 0) {
                 if (dy > 0) {
                     dir = 'N';
                 } else {
                     dir = 'S';
                 }
             } else {
                 if (dx > 0) {
                     dir = 'E';
                 } else {
                     dir = 'W';
                 }
             }
            return Util.getMoveData(robot.heading, robot.getLocation().x,
                    robot.getLocation().y, dir);


    }

    private void squareTrace() {

        int currentX = robot.getNode().getLocation().x;
        int currentY = robot.getNode().getLocation().y;

        nextNode = robot.getArena().nodeMap.get(currentX + "," +
                  ++currentY);

        path.add(nextNode);

        nextNode = robot.getArena().nodeMap.get(++currentX + "," +
                  currentY);

        path.add(nextNode);
        
        nextNode = robot.getArena().nodeMap.get(currentX + "," +
                  --currentY);
        
        path.add(nextNode);

        nextNode = robot.getArena().nodeMap.get(--currentX + "," +
                  currentY);

        path.add(nextNode);
    }
}
